package org.nokatag.system;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.nokatag.password.AESPlus;

public class RunUserFunction{
	@SuppressWarnings("rawtypes")
	public static String RunFunction(String funp,String value,HttpServletRequest request){
			String rv="-1";
			funp=AESPlus.decrypt(funp,Concest.SQLKEY);//
			funp=delEn(funp);//去掉换行符
			String path = funp.substring(0,funp.indexOf("("));//fun:org.sn.s.F(this.value,'s',3);
			String patgf=path;
			path = path.substring(0,path.lastIndexOf("."));//path = com.sn.Scel  类
			String fun = patgf.substring(patgf.lastIndexOf(".")+1,patgf.length());// fun = F 方法
			List<Map<String,Object>> prave = new ArrayList<Map<String,Object>>();
			String snp = funp.substring(funp.indexOf("(")+1,funp.indexOf(")"));// java.long.string id,java.util.map cell,java.util.list row,'yyy-mm'
			//-------------------方法参数--------------------------------------
			int n=0;
			for(String p:snp.split(",")){
				Map<String,Object> ma = new HashMap<String,Object>();
				p = p.trim();
				if(p.length()>0){//有参数
					if(p.startsWith("'") && p.endsWith("'")){
						ma.put("type","java.lang.String");//类型
						ma.put("value",p.substring(1,p.length()-1));//值
					}else if(p.startsWith("this.value")){
						ma.put("type","java.lang.String");//类型
						ma.put("value",value);//值
					}else if(p.startsWith("request")){
						ma.put("type","javax.servlet.http.HttpServletRequest");//类型
						ma.put("value",request);//值
					}else if(p.startsWith(":") && p.indexOf("_")!=-1){//request value
						String vn[]=p.split("_");
						ma.put("type",vn[1]);//类型
						ma.put("value",request.getParameter(vn[0].substring(1)));//值
					}else{
						try{
							ma.put("type","java.lang.Integer");//类型
							ma.put("value",Integer.parseInt(p));//值
						}catch(Exception e){
							try{
								ma.put("type","java.lang.double");//类型
								ma.put("value",Double.parseDouble(p));//值
							}catch(Exception se){
								ma.put("type","java.lang.String");//类型
								ma.put("value",p);//值
							}
						}
					}
					prave.add(n,ma);
					n++;
				}
			}
			//-------------------------------------------------------------
			try{
				//---------------参数类型----------------------------
				Class[] argsClass = new Class[prave.size()];
				for (int a = 0;a<argsClass.length;a++) {
					 if(prave.get(a).get("type").equals("java.lang.String"))
						 argsClass[a]=java.lang.String.class;
					 else if(prave.get(a).get("type").equals("java.lang.Integer")){
						 argsClass[a]=java.lang.Integer.class;
					 }else if(prave.get(a).get("type").equals("java.lang.double")){
						 argsClass[a]=java.lang.Double.class;
					 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
						 argsClass[a]=javax.servlet.http.HttpServletRequest.class;
					 }
				}
				//---------------参数值------------------------------
				Object arglist[] = new Object[prave.size()];
				for (int a = 0;a<argsClass.length;a++) {
					 if(prave.get(a).get("type").equals("java.lang.String")){
						 if(prave.get(a).get("value")==null)
							 arglist[a] = null;
						 else
							 arglist[a] = new String(prave.get(a).get("value").toString());
					 }else if(prave.get(a).get("type").equals("java.lang.Integer")){
						 if(prave.get(a).get("value")==null)
							 arglist[a] = null;
						 else
							 arglist[a] = new Integer(prave.get(a).get("value").toString());
					 }else if(prave.get(a).get("type").equals("java.lang.double")){
						 if(prave.get(a).get("value")==null)
							 arglist[a] = null;
						 else
							 arglist[a] = new Double(prave.get(a).get("value").toString());
					 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
						arglist[a] = request;
					 }
				}
				//-------------------------------------------------------
				NLoadFun nloadfun = new NLoadFun();
				Object obj = nloadfun.getBean(path);
				Method method = obj.getClass().getMethod(fun.trim(), argsClass);
				rv = (String)method.invoke(obj, arglist);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return rv;
	}
	//-----------------------服务器方法调用--------------------------------------------
	@SuppressWarnings("rawtypes")
	public static String SerFunction(String funp,HttpServletRequest request){
		String rv="-1";
		funp=AESPlus.decrypt(funp,Concest.SQLKEY);
		funp=delEn(funp);//去掉换行符
		//----------------------有参数----------------------------------------------
		if(funp.indexOf("(")!=-1 && funp.indexOf(")")!=-1){
			String path = funp.substring(0,funp.indexOf("("));//fun:org.sn.s.F(this.value,'s',3);
			String patgf=path;
			path = path.substring(0,path.lastIndexOf("."));//path = com.sn.Scel  类
			String fun = patgf.substring(patgf.lastIndexOf(".")+1,patgf.length());// fun = F 方法
			List<Map<String,Object>> prave = new ArrayList<Map<String,Object>>();
			String snp = funp.substring(funp.indexOf("(")+1,funp.indexOf(")"));// java.long.string id,java.util.map cell,java.util.list row,'yyy-mm'
			//-------------------方法参数--------------------------------------
			int n=0;
			for(String p:snp.split(",")){//java.lang.String a,java.lang.Integer b
				Map<String,Object> ma = new HashMap<String,Object>();
				p = p.trim();
				if(p.length()>0){//有参数
					String type=p.split(" ")[0];//参数类型
					String pname=p.split(" ")[1];//参数名字
					ma.put("type",type);//类型
					ma.put("value",request.getParameter(pname));//值
					prave.add(n,ma);
					n++;
				}
			}
			//-------------------------------------------------------------
			try{
				//---------------参数类型----------------------------
				Class[] argsClass = new Class[prave.size()];
				for (int a = 0;a<argsClass.length;a++) {
					 if(prave.get(a).get("type").equals("java.lang.String"))
						 argsClass[a]=java.lang.String.class;
					 else if(prave.get(a).get("type").equals("java.lang.Integer")){
						 argsClass[a]=java.lang.Integer.class;
					 }else if(prave.get(a).get("type").equals("java.lang.Double")){
						 argsClass[a]=java.lang.Double.class;
					 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
						 argsClass[a]=javax.servlet.http.HttpServletRequest.class;
					 }
				}
				//---------------参数值------------------------------
				Object arglist[] = new Object[prave.size()];
				for (int a = 0;a<argsClass.length;a++) {
					 if(prave.get(a).get("type").equals("java.lang.String")){
						 if(prave.get(a).get("value")==null)
							 arglist[a] = null;
						 else
							 arglist[a] = new String(prave.get(a).get("value").toString());
					 }else if(prave.get(a).get("type").equals("java.lang.Integer")){
						 if(prave.get(a).get("value")==null)
							 arglist[a] = null;
						 else
							 arglist[a] = new Integer(prave.get(a).get("value").toString());
					 }else if(prave.get(a).get("type").equals("java.lang.Double")){
						 if(prave.get(a).get("value")==null)
							 arglist[a] = null;
						 else
							 arglist[a] = new Double(prave.get(a).get("value").toString());
					 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
						 arglist[a] = request;
					 }
				}
				//-------------------------------------------------------
				NLoadFun nloadFun = new NLoadFun();
				Object obj = nloadFun.getBean(path);
				Method method = obj.getClass().getMethod(fun.trim(), argsClass);
				rv = (String)method.invoke(obj, arglist);
			} catch (Exception e) {
				e.printStackTrace();
			}
		//-------------------------没有参数------------------------------------
		}else{
			try{
				String classname  = funp.substring(0,funp.lastIndexOf("."));//fun:org.sn.s.F;
				String fun  = funp.substring(funp.lastIndexOf(".")+1,funp.length());//fun:org.sn.s.F;
				NLoadFun nloadFun = new NLoadFun();
				Object obj = nloadFun.getBean(classname);
				Method method = obj.getClass().getMethod(fun.trim());
				rv = (String)method.invoke(obj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//=================================================================================
		return rv;
}
	//-----------------------图表调用方法-------------------------------------------------
	@SuppressWarnings("rawtypes")
	public static String StatFunction(String funp,HttpServletRequest request){
		String rv="-1";
		if(funp==null || funp.trim().length()<1)
			return "";
		funp=AESPlus.decrypt(funp,Concest.SQLKEY);
		funp=delEn(funp);//去掉换行符
		String path = funp.substring(0,funp.indexOf("("));//fun:org.sn.s.F(java.lang.String id,java.lang.String 's',java.lang.Integer 3,);
		String patgf=path;
		path = path.substring(0,path.lastIndexOf("."));//path = com.sn.Scel  类
		String fun = patgf.substring(patgf.lastIndexOf(".")+1,patgf.length());// fun = F 方法
		List<Map<String,Object>> prave = new ArrayList<Map<String,Object>>();
		String snp = funp.substring(funp.indexOf("(")+1,funp.indexOf(")"));// java.long.string id,java.util.map cell,java.util.list row,'yyy-mm'
		//-------------------方法参数--------------------------------------
		int n=0;
		for(String p:snp.split(",")){//a,'b'
			Map<String,Object> ma = new HashMap<String,Object>();
			p = p.trim();
			if(p.length()>0){//有参数
				String type=p.split(" ")[0];//参数类型
				String pname=p.split(" ")[1];//参数名字
				String value = request.getParameter(pname);
				if("java.lang.String".equals(type)){//字符串
					ma.put("type",type);//类型
					ma.put("value",value);//值
				}else if("java.lang.Integer".equals(type)){//整数
					ma.put("type",type);//类型
					ma.put("value",null==value?null:Integer.parseInt(value));//值
				}else if("java.lang.Double".equals(type)){
					ma.put("type",type);//类型
					ma.put("value",null==value?null:Integer.parseInt(value));//值
				}else if("javax.servlet.http.HttpServletRequest".equals(type)){
					ma.put("type",type);//类型
					ma.put("value",request);//值
				}else{
					ma.put("type",type);//类型
					ma.put("value",value);//值	
				}
				prave.add(n,ma);
				n++;
			}
		}
		//-------------------------------------------------------------
		try{
			//---------------参数类型----------------------------
			Class[] argsClass = new Class[prave.size()];
			for (int a = 0;a<argsClass.length;a++) {
				 if("java.lang.String".equals(prave.get(a).get("type")))
					 argsClass[a]=java.lang.String.class;
				 else if("java.lang.Integer".equals(prave.get(a).get("type"))){
					 argsClass[a]=java.lang.Integer.class;
				 }else if("java.lang.Double".equals(prave.get(a).get("type"))){
					 argsClass[a]=java.lang.Double.class;
				 }else if("javax.servlet.http.HttpServletRequest".equals(prave.get(a).get("type"))){
					 argsClass[a]=javax.servlet.http.HttpServletRequest.class;
				 }else
					 argsClass[a]=java.lang.Object.class;
			}
			//---------------参数值------------------------------
			Object arglist[] = new Object[prave.size()];
			for (int a = 0;a<argsClass.length;a++) {
				 if("java.lang.String".equals(prave.get(a).get("type"))){
					 if(prave.get(a).get("value")==null)
						 arglist[a] = null;
					 else
						 arglist[a] = new String(prave.get(a).get("value").toString());
				 }else if("java.lang.Integer".equals(prave.get(a).get("type"))){
					 if(prave.get(a).get("value")==null)
						 arglist[a] = null;
					 else
						 arglist[a] = new Integer(prave.get(a).get("value").toString());
				 }else if("java.lang.Double".equals(prave.get(a).get("type"))){
					 if(prave.get(a).get("value")==null)
						 arglist[a] = null;
					 else
						 arglist[a] = new Double(prave.get(a).get("value").toString());
				 }else if("javax.servlet.http.HttpServletRequest".equals(prave.get(a).get("type"))){
					 arglist[a] = request;
				 }
			}
			//-------------------------------------------------------
			NLoadFun nloadFun = new NLoadFun();
			Object obj = nloadFun.getBean(path);
			Method method = obj.getClass().getMethod(fun.trim(), argsClass);
			rv = (String)method.invoke(obj, arglist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rv;
}
	//-----------------------标签调用自定义方法--------------------------------------------
	@SuppressWarnings("rawtypes")
	public static String TagFunction(String funp,HttpServletRequest request,ResultSet rst){
		String rv="-1";
		funp=delEn(funp);//去掉换行符
		String path = funp.substring(0,funp.indexOf("("));//fun:org.sn.s.F(java.lang.String id,java.lang.String 's',java.lang.Integer 3,);
		String patgf=path;
		path = path.substring(0,path.lastIndexOf("."));//path = com.sn.Scel  类
		String fun = patgf.substring(patgf.lastIndexOf(".")+1,patgf.length());// fun = F 方法
		List<Map<String,Object>> prave = new ArrayList<Map<String,Object>>();
		String snp = funp.substring(funp.indexOf("(")+1,funp.indexOf(")"));// java.long.string id,java.util.map cell,java.util.list row,'yyy-mm'
		//-------------------方法参数--------------------------------------
		int n=0;
		for(String p:snp.split(",")){//a,'b'
			Map<String,Object> ma = new HashMap<String,Object>();
			p = p.trim();
			if(p.length()>0){//有参数
				String type=p.split(" ")[0];//参数类型
				String pname=p.split(" ")[1];//参数名字
				if(pname.startsWith("'") && pname.endsWith("'")){
					ma.put("type",type);//类型
					ma.put("value",(p.length()>2?p.split("'")[1]:""));//值
				}else if(type.equals("java.lang.Integer")){
					ma.put("type",type);//类型
					ma.put("value",pname);//值
				}else if(type.equals("java.lang.Double")){
					ma.put("type",type);//类型
					ma.put("value",pname);//值
				}else if(type.equals("javax.servlet.http.HttpServletRequest")){
					ma.put("type",type);//类型
					ma.put("value",pname);//值
				}else{
					try{
						ma.put("type",type);//类型
						ma.put("value",rst.getString(pname));//值
					}catch(Exception se){
						ma.put("type",type);//类型
						ma.put("value","");//值	
					}
				}
				prave.add(n,ma);
				n++;
			}
		}
		//-------------------------------------------------------------
		try{
			//---------------参数类型----------------------------
			Class[] argsClass = new Class[prave.size()];
			for (int a = 0;a<argsClass.length;a++) {
				 if(prave.get(a).get("type").equals("java.lang.String"))
					 argsClass[a]=java.lang.String.class;
				 else if(prave.get(a).get("type").equals("java.lang.Integer")){
					 argsClass[a]=java.lang.Integer.class;
				 }else if(prave.get(a).get("type").equals("java.lang.Double")){
					 argsClass[a]=java.lang.Double.class;
				 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
					 argsClass[a]=javax.servlet.http.HttpServletRequest.class;
				 }
			}
			//---------------参数值------------------------------
			Object arglist[] = new Object[prave.size()];
			for (int a = 0;a<argsClass.length;a++) {
				 if(prave.get(a).get("type").equals("java.lang.String"))
					 arglist[a] = new String(prave.get(a).get("value").toString());
				 else if(prave.get(a).get("type").equals("java.lang.Integer")){
					 arglist[a] = new Integer(prave.get(a).get("value").toString());
				 }else if(prave.get(a).get("type").equals("java.lang.Double")){
					 arglist[a] = new Double(prave.get(a).get("value").toString());
				 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
					 arglist[a] = request;
				 }
			}
			//-------------------------------------------------------
			NLoadFun nloadFun = new NLoadFun();
			Object obj = nloadFun.getBean(path);
			Method method = obj.getClass().getMethod(fun.trim(), argsClass);
			rv = (String)method.invoke(obj, arglist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rv;
}
	@SuppressWarnings("rawtypes")
	public static String TagFunction(String funp,HttpServletRequest request,Map<String, Object> map){
		String rv="-1";
		//-----------------去掉换行符--------------------------------------
		funp=delEn(funp);//去掉换行符
		//----------------------------------------------------------------
		String path = funp.substring(0,funp.indexOf("("));//fun:org.sn.s.F(java.lang.String id,java.lang.String 's',java.lang.Integer 3,);
		String patgf=path;
		path = path.substring(0,path.lastIndexOf("."));//path = com.sn.Scel  类
		String fun = patgf.substring(patgf.lastIndexOf(".")+1,patgf.length());// fun = F 方法
		List<Map<String,Object>> prave = new ArrayList<Map<String,Object>>();
		String snp = funp.substring(funp.indexOf("(")+1,funp.indexOf(")"));// java.long.string id,java.util.map cell,java.util.list row,'yyy-mm'
		//-------------------方法参数--------------------------------------
		int n=0;
		for(String p:snp.split(",")){//a,'b'
			Map<String,Object> ma = new HashMap<String,Object>();
			p = p.trim();
			if(p.length()>0){//有参数
				String type=p.split(" ")[0];//参数类型
				String pname=p.split(" ")[1];//参数名字
				if(pname.startsWith("'") && pname.endsWith("'")){
					ma.put("type",type);//类型
					ma.put("value",(p.length()>2?p.split("'")[1]:""));//值
				}else if(type.equals("java.lang.Integer")){
					ma.put("type",type);//类型
					ma.put("value",pname);//值
				}else if(type.equals("java.lang.Double")){
					ma.put("type",type);//类型
					ma.put("value",pname);//值
				}else if(type.equals("javax.servlet.http.HttpServletRequest")){
					ma.put("type",type);//类型
					ma.put("value",pname);//值
				}else{
					try{
						ma.put("type",type);//类型
						ma.put("value",map.get(pname));//值
					}catch(Exception se){
						ma.put("type",type);//类型
						ma.put("value","");//值	
					}
				}
				prave.add(n,ma);
				n++;
			}
		}
		//-------------------------------------------------------------
		try{
			//---------------参数类型----------------------------
			Class[] argsClass = new Class[prave.size()];
			for (int a = 0;a<argsClass.length;a++) {
				 if(prave.get(a).get("type").equals("java.lang.String"))
					 argsClass[a]=java.lang.String.class;
				 else if(prave.get(a).get("type").equals("java.lang.Integer")){
					 argsClass[a]=java.lang.Integer.class;
				 }else if(prave.get(a).get("type").equals("java.lang.Double")){
					 argsClass[a]=java.lang.Double.class;
				 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
					 argsClass[a]=javax.servlet.http.HttpServletRequest.class;
				 }
			}
			//---------------参数值------------------------------
			Object arglist[] = new Object[prave.size()];
			for (int a = 0;a<argsClass.length;a++) {
				 if(prave.get(a).get("type").equals("java.lang.String")){
					 if(prave.get(a).get("value")==null)
						 arglist[a] = null;
					 else
						 arglist[a] = new String(prave.get(a).get("value").toString());
				 }else if(prave.get(a).get("type").equals("java.lang.Integer")){
					 if(prave.get(a).get("value")==null)
						 arglist[a] = null;
					 else
						 arglist[a] = new Integer(prave.get(a).get("value").toString());
				 }else if(prave.get(a).get("type").equals("java.lang.Double")){
					 if(prave.get(a).get("value")==null)
						 arglist[a] = null;
					 else
						 arglist[a] = new Double(prave.get(a).get("value").toString());
				 }else if(prave.get(a).get("type").equals("javax.servlet.http.HttpServletRequest")){
					 arglist[a] = request;
				 }
			}
			//-------------------------------------------------------
			NLoadFun nloadfun = new NLoadFun();
			Object obj = nloadfun.getBean(path);
			Method method = obj.getClass().getMethod(fun.trim(), argsClass);
			rv = (String)method.invoke(obj, arglist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rv;
}
	/**
	 * 去掉换行符
	 * @param str
	 * @return
	 */
	public static String delEn(String str){
		//-----------------去掉换行符--------------------------------------
		Pattern p = Pattern.compile("\\s*\n");
		Matcher m = p.matcher(str);
		str=m.replaceAll("").trim();
		return str;
		//----------------------------------------------------------------
	}
}
